/**
 * 
 */
package com.netscenery.yang.model;

import java.util.ArrayList;

import org.jdom2.Element;

import com.netscenery.util.ErrTag;
import com.netscenery.yang.designer.LeafFieldMap;
import com.netscenery.yang.designer.ListFieldMap;
import com.netscenery.yang.designer.MapInfo;
import com.netscenery.yang.designer.MethodInfo;
import com.netscenery.yang.designer.Parameter;

/**
 * @author llly
 *
 */
public class Case extends Node {
	public Case(Env env, String value) throws ModelException{
		super(env,value);
	}

	public Validator validate(){
		Validator validator = new Validator();
		if (!(env.getParent() instanceof Choice)){
			validator.setIsValid(false);
			validator.addMsg(new Message(this,ErrTag.ERROR,"case must be son of choice."));
		}
		Choice p = (Choice)(env.getParent());
		ArrayList<Node> actualSons= this.getDataSons();
		if (null == actualSons){
			validator.setIsValid(false);
			validator.addMsg(new Message(this,ErrTag.ERROR,"case must have at least one son."));
		}
		else {
			int size = actualSons.size();
			if (0 == size) {
				validator.setIsValid(false);
				validator.addMsg(new Message(this,ErrTag.ERROR,"case must have at least one son."));

			}

			for (int i = 0; i < size; i++) {
				Node son = actualSons.get(i);
				if (null != son) {
					if (son instanceof Case) {
						validator.setIsValid(false);
						validator.addMsg(new Message(this,ErrTag.ERROR,"case must not have son with case type."));

					}
					// default case must have no mandatory nodes
					if (null != p.getDefaultValue()
							&& p.getDefaultValue().equals(this.name)) {
						
						if (son.isMandatory()) {
							validator.setIsValid(false);
							validator.addMsg(new Message(this,ErrTag.ERROR,"default case must have no mandatory nodes."));

						}
					}

				}
			}
		}
		
		Validator sValidator = super.validate();
		validator.copy(sValidator);
		return validator;
	}

	@Override
	public boolean isMandatory() {
		// TODO Auto-generated method stub
		return false;
	}

	@Override
	public void update() {
		// TODO Auto-generated method stub
		
	}

	
	
	

}
